#include "name.h"
#include <wx/wx.h>
#include <wx/file.h>
#include <string>
#include <vector>
#include "cJSON.h"

Name::Name():name(0),gender(0),usage(0)
{
}

Name::Name(const cJSON* node)
{
	parse(node);
}

Name::~Name()
{
	if(name!=0)
	{
		delete[] name;
		name=0;
	}
	if(gender!=0)
	{
		delete[] gender;
		gender=0;
	}
	if(usage!=0)
	{
		delete[] usage;
		usage=0;
	}
}

void Name::parse(const cJSON* node)
{
	cJSON* childs=0;
	if(node->type==6)
	{
		childs=node->child;
		while(childs!=0)
		{
			if(strcmp(childs->string, "name"))
			{
				name=childs->valuestring;
			}else if(strcmp(childs->string, "gender"))
			{
				gender=childs->valuestring;
			}else if(strcmp(childs->string, "usage"))
			{
				usage=childs->valuestring;
			}
		}
		cJSON_Delete(childs);
		childs=0;
	}
}

cJSON* Name::build()
{
	cJSON* nameObject=cJSON_CreateObject();
	cJSON_AddStringToObject(nameObject,"name",name);
	if(gender!=0)
	{
		cJSON_AddStringToObject(nameObject,"gender",gender);
	}
	cJSON_AddStringToObject(nameObject,"usage",usage);
	return nameObject;
}

char* Name::getName()
{
	return name;
}

void Name::setName(const char* n)
{
	strcpy(name, n);
}

char* Name::getGender()
{
	return gender;
}

void Name::setGender(const char* g)
{
	strcpy(gender, g);
}

char* Name::getUsage()
{
	return usage;
}

void Name::setUsage(const char* u)
{
	strcpy(usage, u);
}

NameController::NameController()
{
	
}

NameController::~NameController()
{
	clear();
}

void NameController::load(const wxString& working_path)
{
	clear();
	wxString nameFilePath=working_path+_T("\names.json");
	if(wxFile::Exists(nameFilePath))
	{
		wxFile nameJson(nameFilePath);
		if(nameJson.IsOpened())
		{
			wxFileOffset fileSize=nameJson.Length();
			char* nameContent=new char[fileSize];
			nameJson.Read((void*)nameContent, fileSize);
			cJSON* allNames=cJSON_Parse(nameContent);
			delete[] nameContent;
			nameContent=0;
			nameJson.Close();
			
			if(allNames->type==cJSON_Array)
			{
				cJSON* arrItem=allNames->child;
				while(arrItem!=0)
				{
					Name* subName=new Name(arrItem->child);
					append(subName);
					subName=0;
					arrItem=arrItem->next;
				}
			}
			
			cJSON_Delete(allNames);
		}
	}
}

void NameController::encode(const wxString& working_path)
{
	cJSON* root=cJSON_CreateArray();
	for(std::vector<Name*>::iterator iter=names.begin();iter!=names.end();++iter)
	{
		cJSON* subObject=(*iter)->build();
		cJSON_AddItemToArray(root,subObject);
	}
	char* nameJsonContent=cJSON_Print(root);
	wxString nameFilePath=working_path+_T("\names.json");
	if(wxFile::Exists(nameFilePath))
	{
		wxFile nameJson(nameFilePath,wxFile::write);
		if(nameJson.IsOpened())
		{
			nameJson.Write(nameJsonContent,sizeof(nameJsonContent));
			nameJson.Close();
		}
	}
	cJSON_Delete(root);
	delete[] nameJsonContent;
}

Name* NameController::get(const int& index)
{
	return names[index];
}

void NameController::append(Name* new_name)
{
	names.push_back(new_name);
}

void NameController::remove(const int& index)
{
	std::vector<Name*>::iterator rIter=names.begin();
	rIter+=index;
	names.erase(rIter);
}

void NameController::clear()
{
	for(std::vector<Name*>::iterator namesIter=names.begin();namesIter!=names.end();namesIter++)
	{
		delete *namesIter;
		*namesIter=0;
	}	
	names.clear();
}